﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StarcraftDemo4;

namespace StarcraftDemo4Site.Controllers
{ 
    public class ResultController : Controller
    {
        private SC2ResultsGamesEntities db = new SC2ResultsGamesEntities();

        //
        // GET: /Result/

        public ViewResult Index()
        {
            return View(db.Results.ToList());
        }


        public ViewResult More(string search_unit)//this is what the controller gets.
        {
            var game_max_marines =
            from result in db.Results
            where result.Marines == db.Results.Max(res => res.Marines)
            select result;

            var game_max_minerals =
            from result in db.Results
            where result.Marines == db.Results.Max(res => res.Minerals)
            select result;

            var game_max_maurader =
            from result in db.Results
            where result.Marines == db.Results.Max(res => res.Marauder)
            select result;
            ViewData["max"] = search_unit;

            if (search_unit.Contains("maurader"))
            {
                return View(game_max_maurader);
            }
            if (search_unit.Contains("marine"))
            {
                return View(game_max_marines); 
            }
            if (search_unit.Contains("mineral"))
            {
                return View(game_max_minerals);
            }
            else { return View(); }
        }

        //
        // GET: /Result/Details/5

        public ViewResult Details(int id)
        {
            Result result = db.Results.Find(id);
            return View(result);
        }

        //Get /Result/Search
        public ActionResult Search()
        {
            StarcraftDemo4Site.Models.Searchq search = new StarcraftDemo4Site.Models.Searchq();
            return View(search);
        }

        //
        // POST: /Result/Search/5

        [HttpPost]
        public ActionResult Search(StarcraftDemo4Site.Models.Searchq search)//this is what is given on post.
        {
            StarcraftDemo4Site.Models.Searcha search_answer = new StarcraftDemo4Site.Models.Searcha();

            if (ModelState.IsValid)
            {
                var search_results =
                   from game in db.Results
                   where game.Marines >= search.marine_lower && game.Marines <= search.marine_upper
                   where game.Marauder >= search.marauder_lower && game.Marauder <= search.marauder_upper
                   where game.Gas >= search.gas_lower && game.Gas <= search.gas_upper
                   where game.Minerals >= search.mineral_lower && game.Minerals <= search.mineral_upper
                   where game.SupplyDepot >= search.supplyd_lower && game.SupplyDepot <= search.supplyd_upper
                   where game.Barracks >= search.barracks_lower && game.Barracks <= search.barracks_upper
                   select game;

                search_answer.list_of_results = search_results;
                search_answer.message = "results from search";
                //ViewData["search_answer"] = search_answer;
                TempData["search_answer"] = search_answer;
                return RedirectToAction("SearchResult");
                //return View();
            }
            return View();//this is whats returned after the submit. probably a page thats blank
        }

        public ActionResult SearchResult()
        {
            var answer = (StarcraftDemo4Site.Models.Searcha)TempData["search_answer"]; 
            return View(answer);
        }

        
        //
        // GET: /Result/Edit/5
 
        public ActionResult Edit(int id)
        {
            Result result = db.Results.Find(id);
            return View(result);
        }

        //
        // POST: /Result/Edit/5

        [HttpPost]
        public ActionResult Edit(Result result)
        {
            if (ModelState.IsValid)
            {
                db.Entry(result).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(result);
        }

        //
        // GET: /Result/Delete/5
 
        public ActionResult Delete(int id)
        {
            Result result = db.Results.Find(id);
            return View(result);
        }

        //
        // POST: /Result/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Result result = db.Results.Find(id);
            db.Results.Remove(result);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
/*
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        */
  }
}